<?php

include dirname( __FILE__ )."/header.php";
$myts =& MyTextSanitizer::getInstance(); // MyTextSanitizer object
include_once dirname( __FILE__ )."/class/mylinkstree.php";
$mytree = new MylinksTree($xoopsDB->prefix($mydirname."_cat"),"cid","pid");
$xoopsOption['template_main'] = $mydirname.'_index.html';
include XOOPS_ROOT_PATH."/header.php";
//wanikoo
$mylinks_module_header = '';
$mylinks_module_header .= '
	<link rel="stylesheet" type="text/css" href="'.mylinksGetStyleURL("mylinks.css").'" />
	<script src="'.mylinksGetStyleURL("mylinks.js").'" type="text/javascript"></script>
	';
$mylinks_module_header = $mylinks_module_header; 
$xoopsTpl->assign('xoops_module_header', $mylinks_module_header);
//

$result=$xoopsDB->query("SELECT cid, title, imgurl FROM ".$xoopsDB->prefix($mydirname."_cat")." WHERE pid = 0 ORDER BY title") or exit("Error");

$count = 1;
while($myrow = $xoopsDB->fetchArray($result)) {
    $imgurl = '';
    if ($myrow['imgurl'] && $myrow['imgurl'] != "http://"){
        $imgurl = $myts->makeTboxData4Edit($myrow['imgurl']);
    }
    $totallink = getTotalItems($myrow['cid'], 1);
    // get child category objects
    $arr = array();
    $arr = $mytree->getFirstChild($myrow['cid'], "title");
    $space = 0;
    $chcount = 0;
    $subcategories = '';
    foreach($arr as $ele){
        $chtitle = $myts->makeTboxData4Show($ele['title']);
        if ($chcount > 5) {
            $subcategories .= "...";
            break;
        }
        if ($space>0) {
            $subcategories .= ", ";
        }
        $subcategories .= "<a href=\"".XOOPSMYLINKURL."/viewcat.php?cid=".$ele['cid']."\">".$chtitle."</a>";
        $space++;
        $chcount++;
    }
    $xoopsTpl->append('categories', array('image' => $imgurl, 'id' => $myrow['cid'], 'title' => $myts->makeTboxData4Show($myrow['title']), 'subcategories' => $subcategories, 'totallink' => $totallink, 'count' => $count));
    $count++;
}
list($numrows) = $xoopsDB->fetchRow($xoopsDB->query("select count(*) from ".$xoopsDB->prefix($mydirname."_links")." where status>0"));
$xoopsTpl->assign('lang_thereare', sprintf(_MD_THEREARE,$numrows));

if ($xoopsModuleConfig['useshots'] == 1) {
    $xoopsTpl->assign('shotwidth', $xoopsModuleConfig['shotwidth']);
    $xoopsTpl->assign('tablewidth', $xoopsModuleConfig['shotwidth'] + 10);
    $xoopsTpl->assign('show_screenshot', true);
    $xoopsTpl->assign('lang_noscreenshot', _MD_NOSHOTS);
}

if ($xoopsUser && $xoopsUser->isAdmin($xoopsModule->mid())) {
    $isadmin = true;
} else {
    $isadmin = false;
}

$xoopsTpl->assign('lang_description', _MD_DESCRIPTIONC);
$xoopsTpl->assign('lang_lastupdate', _MD_LASTUPDATEC);
$xoopsTpl->assign('lang_hits', _MD_HITSC);
$xoopsTpl->assign('lang_rating', _MD_RATINGC);
$xoopsTpl->assign('lang_ratethissite', _MD_RATETHISSITE);
$xoopsTpl->assign('lang_reportbroken', _MD_REPORTBROKEN);
$xoopsTpl->assign('lang_tellafriend', _MD_TELLAFRIEND);
$xoopsTpl->assign('lang_modify', _MD_MODIFY);
$xoopsTpl->assign('lang_latestlistings' , _MD_LATESTLIST);
$xoopsTpl->assign('lang_category' , _MD_CATEGORYC);
$xoopsTpl->assign('lang_visit' , _MD_VISIT);
$xoopsTpl->assign('lang_comments' , _COMMENTS);

$result = $xoopsDB->query("SELECT l.lid, l.cid, l.title, l.url, l.logourl, l.status, l.date, l.hits, l.rating, l.votes, l.comments, t.description FROM ".$xoopsDB->prefix($mydirname."_links")." l, ".$xoopsDB->prefix($mydirname."_text")." t where l.status>0 and l.lid=t.lid ORDER BY date DESC", $xoopsModuleConfig['newlinks'], 0);
while(list($lid, $cid, $ltitle, $url, $logourl, $status, $time, $hits, $rating, $votes, $comments, $description) = $xoopsDB->fetchRow($result)) {
    if ($isadmin) {
        $adminlink = '<a href="'.XOOPSMYLINKURL.'/admin/?op=modLink&amp;lid='.$lid.'"><img src="'.mylinksGetIconURL("editicon.gif").'" border="0" alt="'._MD_EDITTHISLINK.'" /></a>';
    } else {
        $adminlink = '';
    }
    if ($votes == 1) {
        $votestring = _MD_ONEVOTE;
    } else {
        $votestring = sprintf(_MD_NUMVOTES,$votes);
    }
    //$path = $mytree->getPathFromId($cid, "title");
    $path = $mytree->getNicePathFromId2($cid, "title", XOOPSMYLINKURL."/viewcat.php");
    $path = substr($path, 0, -13);
    $path = str_replace("&nbsp;:&nbsp;"," <img src='".mylinksGetIconURL("arrow.gif")."' board='0' alt='' /> ",$path);
    $new = newlinkgraphic($time, $status);
    $pop = popgraphic($hits);
    //$xoopsTpl->append('links', array('id' => $lid, 'cid' => $cid, 'rating' => number_format($rating, 2), 'title' => $myts->makeTboxData4Show($ltitle).$new.$pop, 'category' => $path, 'logourl' => $myts->makeTboxData4Show($logourl), 'updated' => formatTimestamp($time,"m"), 'description' => $myts->makeTareaData4Show($description,0), 'adminlink' => $adminlink, 'hits' => $hits, 'votes' => $votestring, 'comments' => $comments, 'mail_subject' => rawurlencode(sprintf(_MD_INTRESTLINK,$xoopsConfig['sitename'])), 'mail_body' => rawurlencode(sprintf(_MD_INTLINKFOUND,$xoopsConfig['sitename']).':  '.XOOPSMYLINKURL.'/singlelink.php?cid='.$cid.'&lid='.$lid)));
    //by wanikoo
    $xoopsTpl->append('links', array('url' => $myts->makeTboxData4Show($url), 'id' => $lid, 'cid' => $cid, 'rating' => number_format($rating, 2), 'ltitle' => $myts->makeTboxData4Show($ltitle), 'title' => $myts->makeTboxData4Show($ltitle).$new.$pop, 'category' => $path, 'logourl' => $myts->makeTboxData4Show($logourl), 'updated' => formatTimestamp($time,"m"), 'description' => $myts->makeTareaData4Show($description,0), 'adminlink' => $adminlink, 'hits' => $hits, 'votes' => $votestring, 'comments' => $comments, 'mail_subject' => rawurlencode(sprintf(_MD_INTRESTLINK,$xoopsConfig['sitename'])), 'mail_body' => rawurlencode(sprintf(_MD_INTLINKFOUND,$xoopsConfig['sitename']).':  '.XOOPSMYLINKURL.'/singlelink.php?cid='.$cid.'&lid='.$lid)));
}
//wanikoo
$xoopsTpl->assign("lang_feed", "Module Feed ");
//wanikoo theme changer
$xoopsTpl->assign("lang_themechanger", "Theme Changer: ");
$mymylinkstheme_options = '';

    foreach ($GLOBALS['mylinks_allowed_theme'] as $mymylinkstheme) {
        $mymylinkstheme_options .= '<option value="'.$mymylinkstheme.'"';
        if ($mymylinkstheme == $GLOBALS['mylinks_theme']) {
            $mymylinkstheme_options .= ' selected="selected"';
        }
        $mymylinkstheme_options .= '>'.$mymylinkstheme.'</option>';
    }

    $mylinkstheme_select = '<select name="mylinks_theme_select" onchange="submit();" size="1">'.$mymylinkstheme_options.'</select>';

$xoopsTpl->assign("mylinksthemeoption", $mylinkstheme_select);

//wanikoo search
if ( file_exists(XOOPS_ROOT_PATH."/language/".$xoopsConfig['language']."/search.php") ) {
   include_once XOOPS_ROOT_PATH."/language/".$xoopsConfig['language']."/search.php";
} else {
   include_once XOOPS_ROOT_PATH."/language/english/search.php";
}
$xoopsTpl->assign('lang_all', _SR_ALL);
$xoopsTpl->assign('lang_any', _SR_ANY);
$xoopsTpl->assign('lang_exact', _SR_EXACT);
$xoopsTpl->assign('lang_search', _SR_SEARCH);
$xoopsTpl->assign('module_id', $xoopsModule->getVar('mid'));
//category head
$catarray = array();
if ( $mylinks_show_letters ) {
$catarray['letters'] = ml_wfd_letters();
}
if ( $mylinks_show_toolbar ) {
$catarray['toolbar'] = ml_wfd_toolbar();
}
$xoopsTpl->assign('catarray', $catarray);

include_once dirname( __FILE__ )."/footer.php";

?>